<?php
class Admin_Model_DAO_Translation {
    private $_db;
    private static $_instance = null;
    public static $_table = "translation";
    
    /**
     * @return Admin_Model_DAO_Translation
     * */
    public static function factory() {
    	if(self::$_instance == null) {
    		self::$_instance = new self();
    	}
    
    	return self::$_instance;
    }
    
    private function __construct()
    {
    	$module = Utils_Global::get('module');
    	$this->_db = Utils_Global::getDbInstance($module);
    }
    
    /**
     * Insert translation
     * @param array $data
     * @return integer last insert id on success or 0 on failure
     * */
   public function insert($data) {
       $this->_db->insert(self::$_table, $data);
       $result = $this->_db->lastInsertId(self::$_table, 'id');
       return $result;
   }
   
   /**
    * Update translation by given id and data
    * @param integer $id
    * @param array $data
    * @return true on success, false on failure
    * */
   public function update($id, $data) {
       return $this->_db->update(self::$_table, $data, "id = {$this->_db->quote($id, 'INTEGER')}");
   }
   
   /**
    * Update translation by given $i18n, lang and data
    * @param integer $i18n
    * @param array $data
    * @return true on success, false on failure
    * */
   public function updateI18n($i18n, $lang, $data) {
       return $this->_db->update(self::$_table, $data, "i18n_id = {$this->_db->quote($i18n, 'INTEGER')} AND lang = {$this->_db->quote($lang)}");
   }
   
   /**
    * Delete translation by given id
    * @param integer $id
    * @return true on success, false on failure
    * */
   public function delete($id) {
       return $this->_db->delete(self::$_table, "id = {$this->_db->quote($id, 'INTEGER')}");
   }
   
   /**
     * Get list of translation by given options
     * @param array $options
     * @return array
     * */
	public function select($options = array()) {
		$sql = "SELECT * ";
		$from = " FROM `" . self::$_table . "` ";
		$where = " WHERE 1 = 1 ";
		
		if($options['i18n_id']) {
			$where .= " AND `{" . self::$_table . "}`.`i18n_id` = {$this->_db->quote($options['i18n_id'], 'INTEGER')} ";
		}
		
		$result = $this->_db->fetchAll ( $sql . $from . $where );
		return $result;
	}
}